﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WssDemo.ViewModel;
using WssDemo.Data;
using WssDemo.Models;

namespace WssDemo.Controllers
{
    public class UserController : Controller
    {

        private readonly WssDb db = WssDemo.MvcApplication.Db;

        // GET: User
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }


        [HttpPost]
        public ActionResult LoginVtr(LoginModel model)
        {
            
            if (model.User == null || model.PassWord == null)
            {
                return Json(new
                {
                    err = 110,
                    msg = "登录失败，请输入必要的信息"
                });
            }

            var ef = from u in db.Users
                     where u.UserName == model.User
                     select u;

            if (ef.Count() > 0)
            {
                var users = ef.FirstOrDefault();

                if (users.PassWord == model.PassWord)
                {
                    Session["UserName"] = model.User;
                    Session["Id"] = users.Id;

                    return Json(new
                    {
                        status = "ok",
                        msg = "登录成功"
                    });
                }
                else
                {
                    return Json(new
                    {
                        err = 140,
                        msg = "用户名或密码错误，请重试"
                    });
                }

            }
            else
            {
                return Json(new
                {
                    err = 130,
                    msg = "该用户还未注册，请尝试注册"
                });
            }


        }

        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult RegisterVtr(RegisterModel model)
        {
            if (model.User == null || model.PassWord == null || model.ConfimPwd == null)
            {
                return Json(new
                {
                    err = 230,
                    msg = "注册失败，请输入必要的信息"
                });
            }

            if (model.PassWord != model.ConfimPwd)
            {
                return Json(new
                {
                    err = 220,
                    msg = "注册失败，两次输入的密码不一致"
                });
            }

            var ef = from u in db.Users
                     where u.UserName == model.User
                     select u;

            if (ef.Count() > 0)
            {
                return Json(new
                {
                    err = 230,
                    msg = "存在该用户无需注册"
                });
            }
            else
            {
                db.Users.Add(new Users
                {
                    UserName = model.User,
                    PassWord = model.PassWord
                });

                var count = db.SaveChanges();

                if (count > 0)
                {
                    return Json(new
                    {
                        status = "ok",
                        msg = "注册成功"
                    });
                }
                else
                {
                    return Json(new
                    {
                        err = 420,
                        msg = "注册失败"
                    });
                }
            }

        }

    }
}